<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="40">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="content"
            class="input-with-select"
            clearable
            @clear="clearvalue"
            @focus="f"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="initData"
            ></el-button> </el-input
        ></el-col>

        <el-col :span="2"
          ><el-button type="primary" @click="addgoods"
            >添加商品</el-button
          ></el-col
        >
      </el-row>

      <!-- --------------------------- -->
      <el-table :data="tableData" border style="width: 100%" stripe>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="700">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="100">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160">
          <template slot-scope="scope">
            {{ $dayjs(scope.row.add_time).format('YYYY-MM-DD HH:mm:ss') }}=={{
              scope.row.add_time
            }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="btns">
              <el-button
                icon="el-icon-edit"
                size="mini"
                type="primary"
                @click="openDialog('edit', scope.row.goods_id)"
              ></el-button>
              <el-button
                icon="el-icon-delete"
                size="mini"
                type="danger"
                @click="openDialog('del', scope.row.goods_id)"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <p>
        <i class="el-icon-warning" style="color: orange"></i
        ><span>确定删除吗？</span>
      </p>
      <div class="btns">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </el-dialog>

    <!-- 添加组件 -->
  </div>
</template>
<script>
import { goodsListAPI, delgoodsAPI, searchGoodsAPI } from '@/api/goods.js'
export default {
  name: '',
  data() {
    return {
      //当前页数据
      tableData: [],
      currentpage: 1,
      pagesize: 10,
      total: null,
      titleType: null,
      //默认隐藏
      dialogVisible: false,
      fid: null,
      title: null,
      content: '',
      flag: true
    }
  },
  created() {
    this.initData()
  },
  computed: {
  },
  methods: {
    f() {
      console.log(333333);
    },
    //清空表单
    clearvalue() {
      this.initData()
    },
    //添加
    addgoods() {
      this.$router.push('/addgoods').catch(() => { })
    },
    //搜索
    async searchGoods() {

      const res = await searchGoodsAPI({
        query: this.content,
        pagenum: this.currentpage,
        pagesize: this.pagesize
      })
      this.total = res.data.total
      this.tableData = res.data.goods
      // this.initData()
      console.log(res, '+++');
    },

    //
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    //打开对话框
    openDialog(type, id) {
      if (type == 'del') {
        this.dialogVisible = true
        this.titleType = 'del'
        this.title = '删除用户'
        this.fid = id
        console.log(id, 'fid');
      }
    },
    //关闭对话框
    closeDialog() {
      this.dialogVisible = false
    },

    //点击取消按钮
    cancel() {
      this.closeDialog()
    },
    //点击确认按钮
    async submit() {
      if (this.titleType == 'del') {
        //发送请求
        const res = await delgoodsAPI(this.fid)
        console.log(res, '345');
        this.closeDialog()
        this.initData()
      }
    },

    //每一页数量变化
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagesize = val
      this.initData()
    },
    //当前页变化
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentpage = val
      this.initData()
    },
    //渲染
    async initData() {
      if (this.content.trim() == '') {
        const res = await goodsListAPI({
          pagenum: this.currentpage,
          pagesize: this.pagesize
        })
        console.log(res, 'iii');
        this.total = res.data.total
        this.fid = res.data.goods
        this.tableData = res.data.goods
      } else {
        this.searchGoods()
      }



    }
  }
}
</script>
<style lang='less'  scoped>
.el-table {
  font-size: 12px;
}
.box-card {
  margin-top: 12px;
  height: 100%;
}
.el-dialog {
  .btns {
    margin-top: 24px;
  }
}
.el-row {
  margin-bottom: 12px;
}
</style>
